<?php
class TypeDefAction extends ZMAction {
    /**
    * 获取数据类型定义列表
    * 
    */
    public function index(){
        $start = $_REQUEST['start'];
        $limit = $_REQUEST["limit"];
        $sort = $_REQUEST['sort'];
        $dir = $_REQUEST['dir'];
        if(!is_numeric($start)){
            $start = 0;
        }
        if(!is_numeric($limit)){
            $limit = 20;
        }
        $Model = M('ValTypeDef');
        $count = 0;
        $result = $Model->limit("$start,$limit")->select();
        if($result){
            $count = $Model->count();
        }else{
            $result = array();
        }     
        $this->ajaxRetArray($result,$count);
    }
    
    protected function getChilds($Model,$parentid){
        $result = $Model->where("parentid=$parentid")->order('ordernum,vtid')->select();
        if($result){
            foreach($result as &$item){
                $item['id'] = $item['vtid'];
                $item['text'] = $item['typename'];
                $childNodes = $this->getChilds($Model,$item['vtid']);
                if(empty($childNodes)){
                    $item['leaf'] = true;
                }else{
                    $item['expanded'] = true;
                    $item['children'] = $childNodes;
                }
            }
        }else{
            $result = array();
        }
        return $result;     
        
    }
    
    /**
    * 树状Store用
    * 
    */
    public function tree(){
        $Model= M('ValTypeDef');
        $nodes = $this->getChilds($Model,0);
        echo(json_encode($nodes));
    }
    
    
    public function add(){
        
    }
    
    public function del(){
        
    }
    
    public function getEnum(){
        $model = M('ValTypeComboEnum');
        $vtid = $_REQUEST['vtid'];
        $res = $model->field(array('id','val'=>'text'))->where("vtid=$vtid")->select();
        $this->ajaxRetArray($res);
    }
}